package com.cls.business.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.github.pagehelper.PageInfo;
import com.cls.business.entity.LogDeclareTopic;
import com.cls.business.service.ILogDeclareTopicService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 *  Controller
 *
 * @author wandering
 * @date 2020-11-17 09:52:30
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class LogDeclareTopicController extends BaseController {

    private final ILogDeclareTopicService logDeclareTopicService;

    /**
    * 列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/logDeclareTopic")
    public String logDeclareTopicIndex(){
        return MyUtil.view("business/logDeclareTopic/logDeclareTopic");
    }

    /**
    * 新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/logDeclareTopic/logDeclareTopicAdd")
    public String logDeclareTopicAdd(Model model){
        return MyUtil.view("business/logDeclareTopic/logDeclareTopicAdd");
    }

    /**
    * 修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/logDeclareTopic/logDeclareTopicUpdate/{logDeclareTopicId}")
    public String logDeclareTopicUpdate(@PathVariable Long logDeclareTopicId, Model model){
        LogDeclareTopic logDeclareTopic = logDeclareTopicService.getById(logDeclareTopicId);
        model.addAttribute("logDeclareTopic",logDeclareTopic);
        return MyUtil.view("business/logDeclareTopic/logDeclareTopicUpdate");
    }



    /**
    * 详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/logDeclareTopic/logDeclareTopicDetail/{logDeclareTopicId}")
    public String logDeclareTopicDetail(@PathVariable Long logDeclareTopicId, Model model){
        LogDeclareTopic logDeclareTopic = logDeclareTopicService.getById(logDeclareTopicId);
        model.addAttribute("logDeclareTopic",logDeclareTopic);
        return MyUtil.view("business/logDeclareTopic/logDeclareTopicDetail");
    }

    @GetMapping("logDeclareTopic")
    @ResponseBody
    @RequiresPermissions("logDeclareTopic:list")
    public ResponseVO getAllLogDeclareTopics(LogDeclareTopic logDeclareTopic) {
        return ResponseVO.success(logDeclareTopicService.findLogDeclareTopics(logDeclareTopic));
    }

    @GetMapping("logDeclareTopic/list")
    @ResponseBody
//    @RequiresPermissions("logDeclareTopic:list")
    public ResponseVO logDeclareTopicList(QueryRequest request, LogDeclareTopic logDeclareTopic) {
        PageInfo<LogDeclareTopic> pageInfo =  this.logDeclareTopicService.findLogDeclareTopics(request, logDeclareTopic);
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增LogDeclareTopic", exceptionMessage = "新增LogDeclareTopic失败")
    @PostMapping("logDeclareTopic")
    @ResponseBody
    @RequiresPermissions("logDeclareTopic:add")
    public ResponseVO addLogDeclareTopic(@Valid LogDeclareTopic logDeclareTopic) {
        this.logDeclareTopicService.createLogDeclareTopic(logDeclareTopic);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除LogDeclareTopic", exceptionMessage = "删除LogDeclareTopic失败")
    @PostMapping("logDeclareTopic/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("logDeclareTopic:delete")
    public ResponseVO deleteLogDeclareTopics(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.logDeclareTopicService.deleteLogDeclareTopics(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个LogDeclareTopic", exceptionMessage = "删除LogDeclareTopic失败")
    @PostMapping("logDeclareTopic/delete/{logDeclareTopicId}")
    @ResponseBody
    @RequiresPermissions("logDeclareTopic:delete")
    public ResponseVO deleteLogDeclareTopic(@PathVariable Long logDeclareTopicId) {
        this.logDeclareTopicService.removeById(logDeclareTopicId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改LogDeclareTopic", exceptionMessage = "修改LogDeclareTopic失败")
    @PostMapping("logDeclareTopic/update")
    @ResponseBody
    @RequiresPermissions("logDeclareTopic:update")
    public ResponseVO updateLogDeclareTopic(LogDeclareTopic logDeclareTopic) {
        this.logDeclareTopicService.updateLogDeclareTopic(logDeclareTopic);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改LogDeclareTopic", exceptionMessage = "导出Excel失败")
    @PostMapping("logDeclareTopic/excel")
    @ResponseBody
    @RequiresPermissions("logDeclareTopic:export")
    public void export(QueryRequest queryRequest, LogDeclareTopic logDeclareTopic, HttpServletResponse response) {
        List<LogDeclareTopic> logDeclareTopics = this.logDeclareTopicService.findLogDeclareTopics(queryRequest, logDeclareTopic).getList();
        ExcelKit.$Export(LogDeclareTopic.class, response).downXlsx(logDeclareTopics, false);
    }
}
